﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.util;
using System.Data.SqlClient;
using System.Data;
namespace shop.dao
{
    public class UserDao
    {
        public User GetUser(int id)
        {
            string sql = "select id,name,password,regist_status,rank,lastLoginTime  FROM [user] where id = @id";

            SqlParameter[] pms = {
                                 new SqlParameter("@id", id),

                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);

            User User = new User();
            if (dt.Rows.Count>0) {
                EntityAdapt(User, dt.Rows[0]);
            }          
            return User;
        }
        public User GetUserByName(string name)
        {
            string sql = "select id,name,password,regist_status,rank,lastLoginTime  FROM [user] where name = @name";

            SqlParameter[] pms = {
                                 new SqlParameter("@name",name)                            
                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);

            User user = new User();
            if (dt.Rows.Count > 0)
            {
                EntityAdapt(user, dt.Rows[0]);
                return user;
            }
        
            return null;
        }
        public List<User> ListUser(User userSearch)
        {
            string sql = "select id,name,password,regist_status,regist_time,rank,lastLoginTime  FROM [user] where 1 =1";
            SqlParameter[] pms = null;
            if (userSearch == null)
            {
                pms = null;
            }
            else if (userSearch.Rank>0) {
                sql  = sql + " and rank = @rank";
                pms = new SqlParameter[1];
                pms[0] = new SqlParameter("@rank", userSearch.Rank);
            }
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, pms);

            List<User> userList = new List<User>();
            while (reader.Read())
            {
                User user = new User();
                user.Id = (int)reader["id"];
                user.Name = (string)reader["name"];
                user.Password = (string)reader["password"];
                user.Rank = (int)reader["rank"];
                //user.RegistStatus = (int)reader["regist_status"];
                user.LastLoginTime = reader["lastLoginTime"].ToString();
                user.Register_time = reader["regist_time"].ToString();
                userList.Add(user);
            }
            return userList;
        }
        private void EntityAdapt(User user, DataRow row)
        {
            user.Id = (int)row["id"];
            user.Name = (string)row["name"];
            user.Password = (string)row["password"];
            user.Rank = (int)row["rank"];
            user.LastLoginTime = row["lastLoginTime"].ToString();
        }
       
        public bool AddUser(User user)
        {
            string sql = "insert into [user](name,password,regist_status,regist_time,rank) values(@name,@password,1, CONVERT(varchar,GETDATE(),120),@rank)";
            SqlParameter[] pms = {
                                 new SqlParameter("@name", user.Name),
                                 new SqlParameter("@password", user.Password),
                                  new SqlParameter("@rank", user.Rank),
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool UpdateUser(User user)
        {
            string sql = "update [User] set name = @name, password = @password, rank = @rank where  id =@id";
            SqlParameter[] pms = { new SqlParameter("@name", user.Name),
                               new SqlParameter("@password", user.Password),
                               new SqlParameter("@id", user.Id),
                                new SqlParameter("@rank", user.Rank)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool DeleteUser(int id)
        {
            string sql = "delete from [User] where id = @id";
            SqlParameter[] pms = { new SqlParameter("@id", id) };
            int count = 0;
            try
            {
                count = SqlHelper.ExecuteNonQuery(sql, pms);
            }
            catch (Exception e)
            {
                Console.Write(e.Message);

            }
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}